package com.orange.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.handler.AuthResourceDataService;
import com.orange.handler.AuthSchoolService;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.school.AuthSchool;
import com.orange.modules.entity.school_data.AuthResourceData;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/resourceData")
public class AuthResourceDataController extends BaseController{

	private static final Logger log = Logger.getLogger(AuthResourceDataController.class);
	
	@Autowired
	private AuthResourceDataService authResourceDataService;
	
	@Autowired
	private AuthSchoolService authSchoolService;
	
	@RequestMapping(value = "/insertresourceData" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertresourceData(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String sid = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==sid){
				return toResponse(null,ReturnCode.NOLOGIN,"未登陆");
			}
			String subject = (String) request.getAttribute(SessionUser.SESSION_STATUS);//学科
			String remark1 = map.get("remark1");
			String totalLibraryCollection =map.get("totalLibraryCollection");
			String remark2 = map.get("remark2");
			String electronicBooks = map.get("electronicBooks");
			String remark3 = map.get("remark3");
			String relatedProfessionalBooks = map.get("relatedProfessionalBooks");
			String remark4 = map.get("remark4");
			String studentRelatedProfessionalBooks = map.get("studentRelatedProfessionalBooks");
			String remark5 = map.get("remark5");
			String recommendedBook = map.get("recommendedBook");
			String remark6 = map.get("remark6");
			String averageAnnualBook = map.get("averageAnnualBook");
			String remark7 = map.get("remark7");
			String readingroomJournals = map.get("readingroomJournals");
			String remark8= map.get("remark8");
			String studentsFreeReading = map.get("studentsFreeReading");
			String remark9 = map.get("remark9");
			String studentsDigitalResources = map.get("studentsDigitalResources");
			String remark10 = map.get("remark10");
			String professionalJournals = map.get("professionalJournals");
			String remark11 = map.get("remark11");
			String skillsTrainingRoomArea = map.get("skillsTrainingRoomArea");
			String remark12 = map.get("remark12");
			String sharedArea = map.get("sharedArea");
			String remark13 = map.get("remark13");
			String skillsTrainingRoomTypes = map.get("skillsTrainingRoomTypes");
			String remark14 = map.get("remark14");
			String sharedRoom = map.get("sharedRoom");
			String remark15 = map.get("remark15");
			String equipmentCost = map.get("equipmentCost");
			String remark16 = map.get("remark16");
			String sharedDeviceValue = map.get("sharedDeviceValue");
			String status ="0";
			if(totalLibraryCollection == null || StringUtils.isBlank(totalLibraryCollection))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学校藏书总量（纸质图书/册）不能为空");
			if(electronicBooks == null || StringUtils.isBlank(electronicBooks))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"可供学生免费阅读的电子图书（册）不能为空");
			if(relatedProfessionalBooks == null || StringUtils.isBlank(relatedProfessionalBooks))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"相关专业图书数量（册）不能为空");
			if(studentRelatedProfessionalBooks == null || StringUtils.isBlank(studentRelatedProfessionalBooks))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"生均相关专业图书（册）不能为空");
			if(recommendedBook == null || StringUtils.isBlank(recommendedBook))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学生必读书目推荐（册）不能为空");
			if(averageAnnualBook == null || StringUtils.isBlank(averageAnnualBook))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"生均年图书流通率（%）不能为空");
			if(readingroomJournals == null || StringUtils.isBlank(readingroomJournals))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学校阅览室期刊数量（种）不能为空");
			if(studentsFreeReading == null || StringUtils.isBlank(studentsFreeReading))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"可供学生免费阅读电子期刊（种）不能为空");
			if(studentsDigitalResources == null || StringUtils.isBlank(studentsDigitalResources))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"可供学生使用的数字化资源（G)不能为空");
			if(professionalJournals == null || StringUtils.isBlank(professionalJournals))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"专业期刊数量（种）不能为空");
			if(skillsTrainingRoomArea == null || StringUtils.isBlank(skillsTrainingRoomArea))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"教育教学技能实训用房面积（㎡）不能为空");
			if(sharedArea == null || StringUtils.isBlank(sharedArea))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"其中：共享面积不能为空");
			if(skillsTrainingRoomTypes == null || StringUtils.isBlank(skillsTrainingRoomTypes))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"技能实训用房种类（个）不能为空");
			if(sharedRoom == null || StringUtils.isBlank(sharedRoom))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"其中：共享用房不能为空");
			if(equipmentCost == null || StringUtils.isBlank(equipmentCost))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"技能实训设施设备原值（元）不能为空");
			if(sharedDeviceValue == null || StringUtils.isBlank(sharedDeviceValue))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"其中：共享设备值不能为空");
			if(sid == null || StringUtils.isBlank(sid))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学校Id不能为空");
			String remark = remark1+"#"+remark2+"#"+remark3+"#"+remark4+"#"+remark5+"#"+remark6+"#"+remark7+"#"+remark8+"#"+remark9+"#"+remark10+"#"+remark11+"#"+remark12+"#"+remark13+"#"+remark14+"#"+remark15+"#"+remark16;
			AuthResourceData authResourceData = new AuthResourceData();
			authResourceData.setAverageAnnualBook(averageAnnualBook);
			authResourceData.setCreatetime(sdf.format(new Date()));
			authResourceData.setElectronicBooks(electronicBooks);
			authResourceData.setEquipmentCost(equipmentCost);
			authResourceData.setLoginType(subject);
			authResourceData.setProfessionalJournals(professionalJournals);
			authResourceData.setReadingroomJournals(readingroomJournals);
			authResourceData.setRecommendedBook(recommendedBook);
			authResourceData.setRelatedProfessionalBooks(relatedProfessionalBooks);
			authResourceData.setSharedArea(sharedArea);
			authResourceData.setSharedDeviceValue(sharedDeviceValue);
			authResourceData.setSharedRoom(sharedRoom);
			authResourceData.setSid(sid);
			authResourceData.setSkillsTrainingRoomArea(skillsTrainingRoomArea);
			authResourceData.setSkillsTrainingRoomTypes(skillsTrainingRoomTypes);
			authResourceData.setStatus(status);
			authResourceData.setStudentRelatedProfessionalBooks(studentRelatedProfessionalBooks);
			authResourceData.setStudentsDigitalResources(studentsDigitalResources);
			authResourceData.setTotalLibraryCollection(totalLibraryCollection);
			authResourceData.setStudentsFreeReading(studentsFreeReading);
			authResourceData.setLoginType(subject);
			authResourceData.setRemark(remark);
			authResourceDataService.insertAuthResourceData(authResourceData);
			return toResponse(authResourceData,ReturnCode.SUCCESS,"新增成功");
		} catch (Exception e) {
			log.info("新增失败", e);
			return toResponse(null,ReturnCode.FAIL,"新增失败");
		}
		
	}
	
	/**
	 * 根据当前用户查询资源信息
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryResourceDataBySchoolId" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryResourceDataBySchoolId(HttpServletRequest request) throws SQLException{
		String sid = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(null==sid){
			return toResponse(null,ReturnCode.NOLOGIN,"未登陆");
		}
		String subject = (String) request.getAttribute(SessionUser.SESSION_STATUS);//学科
		
		AuthResourceData ard = authResourceDataService.queryAuthResourceDataBySchoolId(sid, subject);
		return toResponse(ard,ReturnCode.SUCCESS,"查询成功");
	}
	
	/**
	 * 修改资源表
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/updateResourceData" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateResourceData(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String sid = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(null==sid){
			return toResponse(null,ReturnCode.NOLOGIN,"未登陆");
		}
		String subject = (String) request.getAttribute(SessionUser.SESSION_STATUS);//学科
		
		try {
			AuthResourceData ard = authResourceDataService.queryAuthResourceDataBySchoolId(sid, subject);
			if(ard!=null){
				String totalLibraryCollection =map.get("totalLibraryCollection");
				String electronicBooks = map.get("electronicBooks");
				String relatedProfessionalBooks = map.get("relatedProfessionalBooks");
				String studentRelatedProfessionalBooks = map.get("studentRelatedProfessionalBooks");
				String recommendedBook = map.get("recommendedBook");
				String averageAnnualBook = map.get("averageAnnualBook");
				String readingroomJournals = map.get("readingroomJournals");
				String studentsFreeReading = map.get("studentsFreeReading");
				String studentsDigitalResources = map.get("studentsDigitalResources");
				String professionalJournals = map.get("professionalJournals");
				String skillsTrainingRoomArea = map.get("skillsTrainingRoomArea");
				String sharedArea = map.get("sharedArea");
				String skillsTrainingRoomTypes = map.get("skillsTrainingRoomTypes");
				String sharedRoom = map.get("sharedRoom");
				String equipmentCost = map.get("equipmentCost");
				String sharedDeviceValue = map.get("sharedDeviceValue");
				String remark1=map.get("remark1");
				String remark2 = map.get("remark2");
				String remark3 = map.get("remark3");
				String remark4 = map.get("remark4");
				String remark5 = map.get("remark5");
				String remark6 = map.get("remark6");
				String remark7 = map.get("remark7");
				String remark8 = map.get("remark8");
				String remark9 = map.get("remark9");
				String remark10 = map.get("remark10");
				String remark11 = map.get("remark11");
				String remark12 = map.get("remark12");
				String remark13 = map.get("remark13");
				String remark14 = map.get("remark14");
				String remark15 = map.get("remark15");
				String remark16 = map.get("remark16");
				if(totalLibraryCollection!=null && StringUtils.isNotBlank(totalLibraryCollection))
					ard.setTotalLibraryCollection(totalLibraryCollection);
				if(electronicBooks!=null && StringUtils.isNotBlank(electronicBooks))
					ard.setElectronicBooks(electronicBooks);
				if(relatedProfessionalBooks!=null && StringUtils.isNotBlank(relatedProfessionalBooks))
					ard.setRelatedProfessionalBooks(relatedProfessionalBooks);
				if(studentRelatedProfessionalBooks!=null && StringUtils.isNotBlank(studentRelatedProfessionalBooks))
					ard.setStudentRelatedProfessionalBooks(studentRelatedProfessionalBooks);
				if(recommendedBook!=null && StringUtils.isNotBlank(recommendedBook))
					ard.setRecommendedBook(recommendedBook);
				if(averageAnnualBook!=null && StringUtils.isNotBlank(averageAnnualBook))
					ard.setAverageAnnualBook(averageAnnualBook);
				if(readingroomJournals!=null && StringUtils.isNotBlank(readingroomJournals))
					ard.setReadingroomJournals(readingroomJournals);
				if(studentsFreeReading!=null && StringUtils.isNotBlank(studentsFreeReading))
					ard.setStudentsFreeReading(studentsFreeReading);
				if(studentsDigitalResources!=null && StringUtils.isNotBlank(studentsDigitalResources))
					ard.setStudentsDigitalResources(studentsDigitalResources);
				if(professionalJournals!=null && StringUtils.isNotBlank(professionalJournals))
					ard.setProfessionalJournals(professionalJournals);
				if(skillsTrainingRoomArea!=null && StringUtils.isNotBlank(skillsTrainingRoomArea))
					ard.setSkillsTrainingRoomArea(skillsTrainingRoomArea);
				if(sharedArea!=null && StringUtils.isNotBlank(sharedArea))
					ard.setSharedArea(sharedArea);
				if(skillsTrainingRoomTypes!=null && StringUtils.isNotBlank(skillsTrainingRoomTypes))
					ard.setSkillsTrainingRoomTypes(skillsTrainingRoomTypes);
				if(sharedRoom!=null && StringUtils.isNotBlank(sharedRoom))
					ard.setSharedRoom(sharedRoom);
				if(equipmentCost!=null && StringUtils.isNotBlank(equipmentCost))
					ard.setEquipmentCost(equipmentCost);
				if(sharedDeviceValue!=null && StringUtils.isNotBlank(sharedDeviceValue))
					ard.setSharedDeviceValue(sharedDeviceValue);
				String remark = remark1+"#"+remark2+"#"+remark3+"#"+remark4+"#"+remark5+"#"+remark6+"#"+remark7+"#"+remark8+"#"+remark9+"#"+remark10+"#"+remark11+"#"+remark12+"#"+remark13+"#"+remark14+"#"
						  + remark15+"#"+remark16;
				ard.setRemark(remark);
				ard.setStatus("1");
				authResourceDataService.insertAuthResourceData(ard);
			}
			return toResponse(ard,ReturnCode.SUCCESS,"修改成功");
		} catch (Exception e) {
			// TODO: handle exception
			return toResponse(null,ReturnCode.FAIL,"修改失败");
		}	
	}
	
	/**********************************************************************************************************/
	/**
	 * 根据当前用户查询资源信息
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryResourceDataBySchoolId1" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryResourceDataBySchoolId1(HttpServletRequest request,@RequestParam String schoolid) throws SQLException{
		try {
			AuthResourceData ard = authResourceDataService.queryAuthResourceDataBySchoolId(schoolid, null);
			return toResponse(ard,ReturnCode.SUCCESS,"查询资源数据成功");
		} catch (Exception e) {
			log.error("查询资源数据失败",e);
			return toResponse(null,ReturnCode.FAIL,"查询资源数据失败");
		}
	}
	
}
